package com.sinocare.system.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sinocare.common.entity.SystemAccount;
import com.sinocare.common.result.Result;
import com.sinocare.common.result.ResultCode;
import com.sinocare.common.vo.AccountVo;
import com.sinocare.common.vo.PassWordVo;
import com.sinocare.system.service.AccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping(value = "/system")
@Api(tags = "系统账号管理")
@CrossOrigin
public class AccountController {
      @Resource
    private AccountService accountService;

    /**
     * 查询所有系统账号(包括模糊条件查询，分页)
     * @param page
     * @param limit
     * @param searchParams
     * @return
     * @throws Exception
     *
     */
    @ApiOperation(value = "查询所有的系统的账号",notes = "详细描述",httpMethod = "GET")
    @RequestMapping(value = "all",method = RequestMethod.GET)
      public Result findAccount(Integer page,Integer limit,String searchParams) throws Exception{
        ObjectMapper objectMapper = new ObjectMapper();
        AccountVo accountVo=null;
        if(searchParams!=null){
            accountVo = objectMapper.readValue(searchParams,AccountVo.class);
        }
            Page page1= PageHelper.startPage(page, limit);
            List<SystemAccount> accounts = accountService.listAccount(accountVo);
            return Result.ok(ResultCode.SUCCESS,page1.getTotal(),accounts);
    }

    /**
     * 停用账号
     * @param id
     * @return
     */
    @ApiOperation(value = "删除账号",notes = "详细描述",httpMethod = "GET")
      @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public Result AccountId(@PathVariable("id") Integer id){
        int i = accountService.updateAccount(id);
        if (i>0){
            return Result.ok(ResultCode.SUCCESS,i);
        }else {
            return Result.fail(ResultCode.FAIL,i);
        }
    }

    /**
     * 修改账号信息
     * @param systemAccount
     * @return
     */
    @ApiOperation(value = "修改账号信息",httpMethod = "GET")
    @RequestMapping(value = "update",method = RequestMethod.GET)
     public Result update(SystemAccount systemAccount){
         int i = accountService.update(systemAccount);
         if (i>0){
            return Result.ok(ResultCode.SUCCESS,i);
         }else{
            return Result.ok(ResultCode.FAIL,i);
         }
     }

    /**
     * 动态获取添加页面的药店下拉框
     * @return
     */
     @ApiOperation(value = "动态获取药店下拉框",httpMethod = "POST")
     @RequestMapping(value = "storeName",method = RequestMethod.POST)
     public Result queryStoreName(){
         List<SystemAccount> systemAccounts = accountService.listStoreName();
       return Result.ok(ResultCode.SUCCESS,systemAccounts);
     }
    /**
     * 动态获取添加页面的角色下拉框
     * @return
     */
     @ApiOperation(value = "动态获取角色下拉框",httpMethod = "POST")
     @RequestMapping(value = "roleName",method = RequestMethod.POST)
     public Result queryRoleName(){
         List<SystemAccount> systemAccounts = accountService.listRoleName();
         System.out.println(systemAccounts);
         return Result.ok(ResultCode.SUCCESS,systemAccounts);
     }

    /**
     * 增加账号
     * @param systemAccount
     * @return
     */
     @ApiOperation(value = "增加账号信息",httpMethod = "POST")
     @RequestMapping(value = "add",method = RequestMethod.POST)
     public Result add(SystemAccount systemAccount){
         System.out.println(systemAccount);
         Random random=new Random();
         int code1= random.nextInt(1000000)+1;
         String customerCode="SN"+code1;
         systemAccount.setCustomerCode(customerCode);
         System.out.println(systemAccount);
         int i = accountService.add(systemAccount);
         if (i>0){
             accountService.addRole(systemAccount);
             accountService.addStore(systemAccount);
         }
         return Result.ok(ResultCode.SUCCESS,i);
     }
     @ApiOperation(value = "重置密码",httpMethod = "GET")
     @RequestMapping(value = "updatePassWord",method = RequestMethod.GET)
     public Result update(PassWordVo passWordVo){
         System.out.println(11111111);
         int i = accountService.updatePassWord(passWordVo);
            if (i>0){
                return Result.ok(ResultCode.SUCCESS,i);
            }else {
                return Result.fail(ResultCode.FAIL,i);
            }
     }
}
